<?php
namespace Home\Controller;
use Home\Common\Common;
use Org\Net\Http;
use Think\Controller;
class ContractinfoController extends Common {
//    人事合同管理
    public function index(){
        $cont = M('contractinfo');
        if(empty($_GET['search'])){
            $where = "";
        }else{
            $where = '`cont_type` like "%'.$_GET['value'].'%" or `preson` like "%'.$_GET['value'].'%"';
        }
        $count=$cont->where($where)->count();
        $page=new \Think\Page($count,5);//实例化Think里面的Page方法，并设置每页显示的数目
        $show=$page->show();
        $cont = $cont->where('start=1',$where)->limit($page->firstRow.','.$page->listRows)->select();
        if(empty($count['preson'])){
            $this->display();
        }
        $this->assign('cont',$cont);
        $this->assign('count',$count);
        $this->assign('show',$show);
        $this->display();
    }
//    添加合同
    public function add(){
        if(empty($_POST['hidden'])){
            $cont_type = M('cont_type');
            $cont_type = $cont_type->select();
            $this->assign('cont_type',$cont_type);
            $this->display();
        }else {
            //循环显示合同类型

            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('doc','docx');// 设置附件上传类型
            $upload->autoSub   =     false;//不生成时间目录
            $upload->savePath  =      ''; // 设置附件上传目录
            $upload->saveName = 'time';//使用时间戳来命名上传的文件
            $cont = M('contractinfo');
            $info   =   $upload->uploadOne($_FILES['file']);
            if(!$info){//错误信息提示
                $this->error($upload->getError());
            }
            //自动验证
            if (!$cont->create()) {
                $this->error($cont->getError());
            }
            $cont->add(array('preson'=>$_POST['person'],'cont_starttime'=>$_POST['cont_starttime'],'cont_endtime'=>$_POST['cont_endtime'],'cont_type'=>$_POST['cont_type'],'cont_content'=>$info['savename'],'stf_id'=>$_POST['stf_id']));
            $this->error('添加成功',U('index'));
            $this->display();
        }
    }
//    合同详情编辑查看
    public function edit(){
        if(empty($_GET['id'])){
            $this->success('参数错误');
        }else{
            $cont = M('contractinfo');
            $cont = $cont->where('cont_id='.$_GET['id'])->find();
            $this->assign('con',$cont);
        }

        $this->display();
    }
//    合同下载
    public function download(){
        if(empty($_GET['id'])){
            $this->error('参数错误请重新尝试');
        }else {
            $res = M('contractinfo')->where(array('cont_id' => $_GET['id']))->find();


            $file_name = $res['cont_content']; //下载文件的名字
            if ($file_name == ""){
                $this->error("文件名获取失败");
            }

//            $file_dir ="C:/Users/Administrator/Desktop/";  //下载文件默认的目录
            $directory = "D:/phpstudy/www/there/Uploads/";

            $fullFile  = $directory.$file_name;
            if (!is_file($fullFile)){
                $this->error("文件不存在");
            }
            header("Content-type:application/octet-stream");
            header("Content-Disposition:attachment;filename = " . $file_name);
            header("Accept-ranges:bytes");
            header("Accept-length:" . filesize($fullFile));
            readfile($fullFile);
//                //判断如果文件存在,则跳转到下载路径
//            if(file_exists('./Uploads'.'/'.$file_name)){
//                $file = fopen($file_dir.$file_name,'rb');//以二进制文件打开
//                Header ( "Content-type: application/octet-stream" );//设置文件流格式
//                Header ( "Accept-Ranges: bytes" );//请求范围的度量单位
//                Header ( "Accept-Length: " . filesize ( $file_dir . $file_name ) );
//                Header ( "Content-Disposition: attachment; filename=" . $file_name );
//                echo fread ( $file, filesize ( $file_dir . '/'.$file_name ) );
//                fclose ( $file );
//                exit ();
//            }else{
//                header('HTTP/1.1 404 Not Found');
//            }
        }
    }

//    合同类型查看
    public function type_of_contract(){
        $cont = M('cont_type');
        $cont = $cont->select();
        $this->assign('cont',$cont);
        $this->display();
    }
//      合同隐藏
    public function del(){
        if(empty($_GET['id'])){
            $this->error("参数错误，请重新尝试",U('index'));
        }
        $contractinfo = M('contractinfo');
        $data['start'] = '0';
        $con = $contractinfo->where(array('cont_id'=>$_GET['id']))->save($data);
        if($con){
            $this->success('数据隐藏成功');
        }else {


            $this->error('数据隐藏失败');
        }
    }
//    显示被隐藏的数据
    public function index_hs(){
        $salary = M('salary');
        $cont = $salary->where('start=0')->select();
        $this->assign('cont',$cont);
        $this->display();
    }
//    回收站彻底删除数据
    public function  del_cd(){
        if(empty($_GET['id'])){
            $this->error('参数错误');
        }else {
            $salary = M('salary');
            $cont = $salary->where(array('lid'=>$_GET['id']))->delete();
            if($cont){
                $this->success('删除成功');
            }else{
                $this->success('删除失败，请重新尝试');
            }
        }
    }
//    回收站数据恢复
    public function del_hs(){
        if(empty($_GET['id'])){
            $this->error("参数错误，请重新尝试",U('index'));
        }
        $salary = M('salary');
        $data['start'] = '1';
        $con = $salary->where(array('lid'=>$_GET['id']))->save($data);
        if($con){
            $this->success('数据恢复成功');
        }else {


            $this->error('数据恢复失败');
        }
    }
}